package ve.edu.ucla.dals.servicio.server;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class JsonServletResponseHelper implements ServletResponseHelper {
	private final int httpStatus;
	private final String contents;
	
	public JsonServletResponseHelper(int httpStatus, JSONObject contents) {
		super();
		this.httpStatus = httpStatus;
		this.contents = contents.toJSONString();
	}

	public JsonServletResponseHelper(int httpStatus, JSONArray contents) {
		super();
		this.httpStatus = httpStatus;
		this.contents = contents.toJSONString();
	}

	public int getHttpStatus() {
		return httpStatus;
	}

	public String getContents() {
		return contents;
	}

	@Override
	public void apply(HttpServletResponse response) throws IOException {
		response.setContentType("application/json");
		response.setStatus(httpStatus);
		response.getWriter().println(contents);
	}
}
